#!/bin/sh /etc/rc.common

START=99
USE_PROCD=1
PROG=/usr/bin/ninja

get_config() {
	config_get_bool enabled $1 enabled 0
	config_get_bool tb_enable $1 tb_enable 0
	config_get level $1 level "info"
	config_get bind $1 bind "0.0.0.0:7999"
	config_get workers $1 workers '1'
	config_get proxies $1 proxies ''
	config_get interface $1 interface ''
	config_get ipv6_subnet $1 ipv6_subnet ''
	config_get_bool disable_direct $1 disable_direct 1
	config_get timeout $1 timeout '600'
	config_get connect_timeout $1 connect_timeout '60'
	config_get tcp_keepalive $1 tcp_keepalive "5"
	config_get tls_cert $1 tls_cert ''
	config_get tls_key $1 tls_key ''
	config_get auth_key $1 auth_key ''
	config_get arkose_gpt3_har_dir $1 arkose_gpt3_har_dir ''
	config_get arkose_gpt4_har_dir $1 arkose_gpt4_har_dir ''
	config_get arkose_auth_har_dir $1 arkose_auth_har_dir ''
	config_get arkose_platform_har_dir $1 arkose_platform_har_dir ''
	config_get arkose_har_upload_key $1 arkose_har_upload_key ''
	config_get arkose_solver_key $1 arkose_solver_key ''
	config_get arkose_solver $1 arkose_solver ''
	config_get_bool arkose_gpt3_experiment $1 arkose_gpt3_experiment 0
	config_get tb_store_strategy $1 tb_store_strategy "mem"
	config_get tb_redis_url $1 tb_redis_url 'redis://127.0.0.1:6379'
	config_get tb_capacity $1 tb_capacity "60"
	config_get tb_fill_rate $1 tb_fill_rate "1"
	config_get tb_expired $1 tb_expired "86400"
	config_get_bool cookie_store $1 cookie_store 0
	config_get concurrent_limit $1 concurrent_limit ""
	config_get_bool disable_webui $1 disable_webui 0
	config_get cf_site_key $1 cf_site_key ""
	config_get cf_secret_key $1 cf_secret_key ""
	config_get pool_idle_timeout $1 pool_idle_timeout "90"
	config_get pbind $1 pbind ""
	config_get pupstream $1 pupstream ""
	config_get pkey $1 pkey ""
	config_get pcert $1 pcert ""
}

start_service() {
	config_load ninja
	config_foreach get_config ninja
	[ $enabled -ne 1 ] && return 1

    args=""
    
	if [ -n "$level" ]; then
		args="$args --level $level"
	fi

	if [ -n "$bind" ]; then
		args="$args --bind $bind"
	fi

	if [ -n "$workers" ]; then
		args="$args --workers $workers"
	fi

	if [ -n "$proxies" ]; then
		args="$args --proxies $proxies"
	fi

	if [ -n "$interface" ]; then
		args="$args --interface $interface"
	fi

	if [ -n "$ipv6_subnet" ]; then
		args="$args --ipv6-subnet $ipv6_subnet"
	fi

	if [ $disable_direct -ne 0 ]; then
		args="$args --disable-direct"
	fi

	if [ -n "$timeout" ]; then
		args="$args --timeout $timeout"
	fi

	if [ -n "$connect_timeout" ]; then
		args="$args --connect-timeout $connect_timeout"
	fi

	if [ -n "$tcp_keepalive" ]; then
		args="$args --tcp-keepalive $tcp_keepalive"
	fi

	if [ -n "$auth_key" ]; then
		args="$args --auth-key $auth_key"
	fi

	if [ -n "$arkose_gpt3_har_dir" ]; then
		args="$args --arkose-gpt3-har-dir $arkose_gpt3_har_dir"
	fi

	if [ -n "$arkose_gpt4_har_dir" ]; then
		args="$args --arkose-gpt4-har-dir $arkose_gpt4_har_dir"
	fi

	if [ -n "$arkose_auth_har_dir" ]; then
		args="$args --arkose-auth-har-dir $arkose_auth_har_dir"
	fi

	if [ -n "$arkose_platform_har_dir" ]; then
		args="$args --arkose-platform-har-dir $arkose_platform_har_dir"
	fi
	
	if [ -n "$arkose_har_upload_key" ]; then
		args="$args --arkose-har-upload-key $arkose_har_upload_key"
	fi
	
	if [ -n "$arkose_solver" ]; then
		args="$args --arkose-solver $arkose_solver"
	fi

	if [ -n "$arkose_solver_key" ]; then
		args="$args --arkose-solver-key $arkose_solver_key"
	fi

	if [ $arkose_gpt3_experiment -ne 0 ]; then
		args="$args --arkose-gpt3-experiment"
	fi
	
	if [ -n "$tls_cert" ]; then
		args="$args --tls-cert $tls_cert"
	fi

	if [ -n "$tls_key" ]; then
		args="$args --tls-key $tls_key"
	fi

	if [ $tb_enable -ne 0 ]; then 
		args="$args --tb-enable"
		if [ -n "$tb_redis_url" ]; then
		    args="$args --tb-redis-url $tb_redis_url"
        fi
		if [ -n "$tb_store_strategy" ]; then
		    args="$args --tb-store-strategy $tb_store_strategy"
        fi
		if [ -n "$tb_capacity" ]; then
		    args="$args --tb-capacity $tb_capacity"
        fi
		if [ -n "$tb_fill_rate" ]; then
		    args="$args --tb-fill-rate $tb_fill_rate"
        fi
		if [ -n "$tb_expired" ]; then
		    args="$args --tb-expired $tb_expired"
        fi
	fi

	if [ $cookie_store -ne 0 ]; then 
		args="$args --cookie-store"
	fi

	if [ -n "$concurrent_limit" ]; then
		args="$args --concurrent-limit $concurrent_limit"
	fi
	
	if [ $disable_webui -ne 0 ]; then 
		args="$args --disable-webui"
	fi

	if [ -n "$cf_site_key" ]; then
		args="$args --cf-site-key $cf_site_key"
	fi

	if [ -n "$cf_secret_key" ]; then
		args="$args --cf-secret-key $cf_secret_key"
	fi

	if [ -n "$pool_idle_timeout" ]; then
		args="$args --pool-idle-timeout $pool_idle_timeout"
	fi

	if [ -n "$pbind" ]; then
		args="$args --pbind $pbind"
	fi

	if [ -n "$pupstream" ]; then
		args="$args --pupstream $pupstream"
	fi

	if [ -n "$pkey" ]; then
		args="$args --pkey $pkey"
	fi

	if [ -n "$pcert" ]; then
		args="$args --pcert $pcert"
	fi

	procd_open_instance
	procd_set_param command /bin/sh -c "$PROG run $args >>/var/log/ninja.log 2>&1"
	procd_set_param stdout 0
	procd_set_param stderr 0
	procd_set_param pidfile /var/run/ninja.pid
	procd_close_instance
}

service_triggers() {
	procd_add_reload_trigger "ninja"
}

reload_service() {
	stop
	for i in $(seq 1 60); do
		if [ -z "$(ps -ef | grep ninja | grep -v grep | grep -v $$ | grep -v '/etc/rc.common' | grep -v '/sbin/service' | grep -v '/sbin/luci-reload')" ]; then
			break
		fi
		sleep 1
	done
	start
}